
import LGXCOM from './common/common'
import { CoordTRFUtil } from './common/tools/coordtool.js'
import { CommonSvc } from './service/commonsvc.js';
import utilfuncs from './common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;
import { DisplayGrid } from './layer/displaygrid';


class DisplayMng {
  constructor(w, h) {
    this.width = w;
    this.height = h;
    console.log("wid=", this.width, "hgt=", this.height);

    //用于绘制真实图形数据的画布（缓冲画布），离屏绘制后贴到前端
    this.bufferCanvas = document.createElement("canvas");
    this.bufferCanvas.setAttribute("width", this.width);
    this.bufferCanvas.setAttribute("height", this.height);
    this.bufferMap = this.bufferCanvas.getContext('2d', { willReadFrequently: true });

    //拖动画布漫游时，重绘区域使用的画布，绘制完厚贴到缓冲画布
    this.panBufferCanvas = document.createElement("canvas");
    this.panBufferCanvas.setAttribute("width", this.width);
    this.panBufferCanvas.setAttribute("height", this.height);
    this.panBufferMap = this.panBufferCanvas.getContext('2d', { willReadFrequently: true });

    //专门用于绘制底图的画布，如画布尺寸示意边框、网格线、背景使用图片时，每次都是全刷新
    this.backGroundCanvas = document.createElement("canvas");
    this.backGroundCanvas.setAttribute("width", this.width);
    this.backGroundCanvas.setAttribute("height", this.height);
    this.backGroundMap = this.backGroundCanvas.getContext('2d', { willReadFrequently: true });
  }

  processImageData(destCanvas, srcCanvas, width, height, blockSize) {
    for (let y = 0; y < height; y += blockSize) {
      for (let x = 0; x < width; x += blockSize) {
        let imageData = srcCanvas.getImageData(x, y, blockSize, blockSize);
        // 处理图像数据
        destCanvas.putImageData(imageData, x, y);
      }
    }
  }

  resizeMap(mapwid, maphgt) {
    this.width = mapwid;
    this.height = maphgt;
    this.bufferCanvas.setAttribute("width", this.width);
    this.bufferCanvas.setAttribute("height", this.height);

    this.panBufferCanvas.setAttribute("width", this.width);
    this.panBufferCanvas.setAttribute("height", this.height);

    this.backGroundCanvas.setAttribute("width", this.width);
    this.backGroundCanvas.setAttribute("height", this.height);
  }

  rePaint(actmap, datamng, clearKind) {
    if (!actmap || !datamng) {
      return;
    }
    let mapInfo = datamng.getMapViewInfo();
    if (mapInfo.paint4PngExport) {
      return;
    }

    let paintMap = this.bufferMap;
    let refreshRect_world = new LGXCOM.LGXRect();
    if (clearKind == 1) {
      refreshRect_world = mapInfo.getViewPort();
    }
    else {
      refreshRect_world = datamng.calRefreshRect();
      if (!refreshRect_world) {
        this.rePaintAuxGraphs(actmap, datamng);
        return;
      }
      datamng.clearFreshZone();
    }

    let t0 = +new Date();
    let refreshRect_screen = CoordTRFUtil.transWorldRect2Screen(refreshRect_world, mapInfo);
    refreshRect_screen.left = Math.floor(refreshRect_screen.left);
    refreshRect_screen.bottom = Math.floor(refreshRect_screen.bottom);
    refreshRect_screen.right = Math.ceil(refreshRect_screen.right);
    refreshRect_screen.top = Math.ceil(refreshRect_screen.top);

    let tmp = refreshRect_screen.top;
    refreshRect_screen.top = refreshRect_screen.bottom;
    refreshRect_screen.bottom = tmp;
    paintMap.clearRect(refreshRect_screen.left, refreshRect_screen.top, refreshRect_screen.width(), refreshRect_screen.height());
    this.displayBackGround(this.backGroundMap, mapInfo);
    var layerMng = datamng.getLayerManager();
    let subDataMngLst = datamng.getSubDataMngs();
    let t4 = +new Date();
    console.log("timeelapse4:" + (t4 - t0));

    let t5 = +new Date();
    console.log("draw graphs timeelapse5:" + (t5 - t0));

    let dragTempGraphList = datamng.m_pDragTempGraphList;
    if (dragTempGraphList) {
      for (let i = 0; i < dragTempGraphList.length; i++) {
        let graph = dragTempGraphList[i];
        console.log("display temp drag graph**************");
        graph.display(paintMap, mapInfo);
      }
    }

    if (mapInfo._disScope) {
      let list = datamng.tSearchNodeStack;
      if (list) {
        for (let i = 0; i < list.length; i++) {
          let node = list[i];
          this.drawFocusPoint(paintMap, mapInfo, node.pt);
        }
      }
    }


    let blockSize = 100; // 块大小
    if (clearKind == 1) {
      layerMng.display(paintMap, mapInfo, refreshRect_world);

      this.backGroundMap.save();
      this.backGroundMap.globalCompositeOperation = 'source-over';
      this.backGroundMap.drawImage(this.bufferCanvas, 0, 0);
      layerMng.displayAuxGraphs(subDataMngLst, this.backGroundMap, mapInfo);
      this.dislayTopoErrorGraph(this.backGroundMap, mapInfo, datamng);
      if (mapInfo.getShowCustCursor()) {
        this.displayCustCursor(this.backGroundMap, mapInfo);
      }
      actmap.drawImage(this.backGroundCanvas, 0, 0);
      this.backGroundMap.restore();
    }
    else {
      this.panBufferMap.clearRect(0, 0, this.width, this.height);
      layerMng.display(this.panBufferMap, mapInfo, refreshRect_world);

      this.bufferMap.save();
      this.bufferMap.globalCompositeOperation = 'source-over';
      this.bufferMap.beginPath();
      this.bufferMap.rect(refreshRect_screen.left, refreshRect_screen.top, refreshRect_screen.width(), refreshRect_screen.height());
      this.bufferMap.clip();
      this.bufferMap.drawImage(this.panBufferCanvas, 0, 0);
      this.bufferMap.restore();

      this.backGroundMap.save();
      this.backGroundMap.globalCompositeOperation = 'source-over';
      this.backGroundMap.drawImage(this.bufferCanvas, 0, 0);
      layerMng.displayAuxGraphs(subDataMngLst, this.backGroundMap, mapInfo);
      this.dislayTopoErrorGraph(this.backGroundMap, mapInfo, datamng);
      if (mapInfo.getShowCustCursor()) {
        this.displayCustCursor(this.backGroundMap, mapInfo);
      }
      this.processImageData(actmap, this.backGroundMap, this.width, this.height, blockSize);
      this.backGroundMap.restore();
    }

    let t1 = +new Date();
    console.log("paint 2 Screen timeelapse:" + (t1 - t0));
  }

  paintPanBufView(datamng, bufRect_world) {
    var layerMng = datamng.getLayerManager();
    let mapInfo = datamng.getMapViewInfo();
    layerMng.display(this.panBufferMap, mapInfo, bufRect_world);
  }


  panView(painter, datamng, screenx, screeny) {
    let mapInfo = datamng.getMapViewInfo();
    mapInfo.setClearViewPortFlag(true);

    let viewPortRect = mapInfo.getViewPort();

    //缓存上一次绘图结果的画布本次漫游复制到前端画布的参数
    let pastLeft = 0;
    let pasteTop = 0;
    let pasteWidth = this.width;
    let pasteHeight = this.height;

    let scaling = datamng.getScaling();
    let worldtx = screenx / scaling;
    let worldty = screeny / scaling;

    let hornBufRect = new LGXCOM.LGXRect();
    hornBufRect.top = viewPortRect.top;
    hornBufRect.bottom = viewPortRect.bottom;

    const repainScale = 2;
    //本次重绘部分复制到显示前端画布的参数
    let bufCopyLeft = 0;//从本次重绘画布的剪切重绘部分到前端画布的水平方向方向
    let bufCopyTop = 0;
    let bufPastLeft = 0;//
    let bufPastTop = 0;
    if (screenx < 0) {
      //向左移动，右侧被拖进视口的区域要重绘
      pastLeft = screenx;
      pasteWidth = this.width - Math.abs(screenx);
      bufCopyLeft = pasteWidth;
      bufPastLeft = pasteWidth;

      let tmpScreenPt = { x: this.width, y: 0 };
      let tmpWorldPt = CoordTRFUtil.screen2world(tmpScreenPt, mapInfo);

      hornBufRect.left = tmpWorldPt.x;
      hornBufRect.right = hornBufRect.left + repainScale * Math.ceil(Math.abs(worldtx));
    }
    else if (screenx > 0) {
      //向右移动，左侧被拖进视口的区域要重绘
      pastLeft = screenx;
      pasteWidth = this.width - Math.abs(screenx);

      bufCopyLeft = 0;
      bufPastLeft = 0;

      hornBufRect.left = viewPortRect.left - worldtx;
      hornBufRect.right = hornBufRect.left + repainScale * Math.ceil(Math.abs(worldtx));
    }

    let vertBufRect = new LGXCOM.LGXRect();
    vertBufRect.left = viewPortRect.left;
    vertBufRect.right = viewPortRect.right;
    if (screeny > 0) {
      //向下拖动，顶部被拖进视口区域的部分要重绘
      pasteTop = screeny;
      pasteHeight = this.height - screeny;

      bufCopyTop = 0;
      bufPastTop = 0;

      let tmpScreenPt = { x: 0, y: 0 };
      let tmpWorldPt = CoordTRFUtil.screen2world(tmpScreenPt, mapInfo);

      vertBufRect.bottom = tmpWorldPt.y;
      vertBufRect.top = vertBufRect.bottom + repainScale * Math.ceil(Math.abs(worldty));
    }
    else if (screeny < 0) {
      //向上拖动，底部被拖进视口区域的部分要重绘
      pasteTop = screeny;
      pasteHeight = this.height - Math.abs(screeny);

      bufCopyTop = pasteHeight;
      bufPastTop = pasteHeight;

      let tmpScreenPt = { x: 0, y: this.height };
      let tmpWorldPt = CoordTRFUtil.screen2world(tmpScreenPt, mapInfo);

      vertBufRect.top = tmpWorldPt.y;
      vertBufRect.bottom = vertBufRect.top - repainScale * Math.ceil(Math.abs(worldty));
    }

    this.downloadImage(this.bufferCanvas, 'buff0');
    this.panBufferMap.clearRect(0, 0, this.width, this.height);
    this.panBufferMap.drawImage(this.bufferCanvas, pastLeft, pasteTop);
    this.bufferMap.clearRect(0, 0, this.width, this.height);
    this.bufferMap.drawImage(this.panBufferCanvas, 0, 0);

    viewPortRect.left -= worldtx;
    viewPortRect.right -= worldtx;
    viewPortRect.top += worldty;
    viewPortRect.bottom += worldty;
    mapInfo.setViewPort(viewPortRect.left, viewPortRect.top, viewPortRect.right, viewPortRect.bottom);
    this.displayBackGround(this.backGroundMap, mapInfo);
    this.downloadImage(this.bufferCanvas, 'buff');
    this.panBufferMap.clearRect(0, 0, this.width, this.height);
    if (Math.abs(screenx) > 0) {
      console.log("hornBufRect:");
      console.log(hornBufRect);
      this.paintPanBufView(datamng, hornBufRect);
      this.downloadImage(this.panBufferCanvas, 'panbuff');
      let hornGap = Math.abs(screenx);
      if (hornGap == 0) {
        console.log("");
      }
      try {
        let imageData = this.panBufferMap.getImageData(bufCopyLeft, 0, hornGap, pasteHeight);
        this.bufferMap.clearRect(bufPastLeft, 0, hornGap, pasteHeight);
        this.bufferMap.putImageData(imageData, bufPastLeft, 0);
      }
      catch (error) {
        console.log(error);
      }
      finally {
        console.log('');
      }
    }

    //纵向移动区域
    if (Math.abs(screeny) > 0) {
      console.log("vertBufRect:");
      console.log(vertBufRect);
      this.paintPanBufView(datamng, vertBufRect);
      this.downloadImage(this.panBufferCanvas, 'panbuff2');
      let vertGap = Math.abs(screeny);
      if (vertGap == 0) {
        console.log("");
      }
      try {
        let imageData = this.panBufferMap.getImageData(0, bufCopyTop, pasteWidth, vertGap);
        this.bufferMap.clearRect(0, bufPastTop, pasteWidth, vertGap);
        this.bufferMap.putImageData(imageData, 0, bufPastTop);
      }
      catch (error) {
        console.log(error);
      }
      finally {
        console.log('');
      }
    }


    painter.clearRect(0, 0, this.width, this.height);
    this.backGroundMap.save();
    this.backGroundMap.globalCompositeOperation = 'source-over';
    this.backGroundMap.drawImage(this.bufferCanvas, 0, 0);
    this.processImageData(painter, this.backGroundMap, this.width, this.height, 100);
    this.backGroundMap.restore();

    mapInfo.setClearViewPortFlag(true);
  }



  downloadImage(painter, name) {
    if (name) {
      return;
    }
    let imagedata = painter.toDataURL("image/png", 0.5);
    console.log(imagedata);

    var oA = document.createElement("a");
    oA.download = name + '.png';// 设置下载的文件名，默认是'下载'
    oA.href = imagedata;
    document.body.appendChild(oA);
    oA.click();
    oA.remove(); // 下载之后把创建的元素删除
  }

  dislayTopoErrorGraph(map, mapInfo, datamng) {
    let list = datamng.getFillTopoErrorUnitList();
    if (!list) {
      return;
    }
    datamng.m_flashTime++;
    if (datamng.m_flashTime > 300) {
      datamng.clearFillTopoErrorUnitList();
      datamng.m_flashTime = 0;
      return;
    }

    let lightFlag = false;
    let t = datamng.m_flashTime % 2;
    if (t == 1) {
      lightFlag = true;
    }

    map.save();
    for (let i = 0; i < list.length; i++) {
      let unit = list[i];
      this.splashErrorUnit(unit, map, mapInfo, lightFlag);
    }
    map.restore();
  }

  splashErrorUnit(unit, map, mapInfo, lightFlag) {
    map.beginPath();
    map.lineWidth = 1;
    if (lightFlag) {
      map.strokeStyle = "#FFff00";
    }
    else {
      map.strokeStyle = "#000000";
    }

    var scrnPt1 = CoordTRFUtil.world2Screen(unit.pt1, mapInfo);
    var scrnPt2 = CoordTRFUtil.world2Screen(unit.pt2, mapInfo);
    map.beginPath();
    map.moveTo(scrnPt1.x, scrnPt1.y);
    map.lineTo(scrnPt2.x, scrnPt2.y);
    map.stroke();
  }

  drawFocusPoint(map, mapInfo, pt) {
    var p0 = CoordTRFUtil.world2Screen(pt, mapInfo);
    let x = p0.x;
    let y = p0.y;
    const size = 20;
    map.fillStyle = "#ff00ff";
    map.beginPath();
    map.lineWidth = 1;
    map.strokeStyle = "#ff00ff";
    map.fillRect(x - size / 2, y - size / 2, size, size);
    map.stroke();
    map.closePath();
    map.fill();
  }

  displayBackGround(map, mapInfo) {
    let width = mapInfo.getMapWidth();
    let height = mapInfo.getMapHeight();
    let backColor = mapInfo.getBackColor();
    map.fillStyle = backColor.toHexString();
    map.beginPath();
    map.fillRect(0, 0, width, height);
    map.closePath();

    DisplayGrid.disPlayGrid(map, mapInfo);
    this.paintMapZoneRect(map, mapInfo);
    this.displayXAxis(map, mapInfo);
    this.displayYAxis(map, mapInfo);
  }

  paintMapZoneRect(map, mapInfo) {
    if (mapInfo.editMode != 'autocad') {
      var x = 0;
      var y = 0;
      var sysLeftBotPt = { x, y };
      var leftBot = CoordTRFUtil.world2Screen(sysLeftBotPt, mapInfo);

      x = this.width;
      y = this.height;
      var sysRightTopPt = { x, y };
      var rightTop = CoordTRFUtil.world2Screen(sysRightTopPt, mapInfo);

      map.beginPath();
      map.lineWidth = 2;
      map.strokeStyle = "#ff00ff";

      map.moveTo(leftBot.x, leftBot.y);
      map.lineTo(rightTop.x, leftBot.y);
      map.lineTo(rightTop.x, rightTop.y);
      map.lineTo(leftBot.x, rightTop.y);
      map.lineTo(leftBot.x, leftBot.y);

      map.stroke();
      map.closePath();
    }
  }


  displayXAxis(map, mapInfo) {
    const axisLenth = 50;
    const axisOffGap = 10;
    const axisLnWid = 3;
    map.save();

    map.beginPath();
    map.lineWidth = 1;
    map.strokeStyle = "#ff0000";

    var x = 0;
    var y = 0;
    var point0 = { x, y };
    var scrnPt1 = CoordTRFUtil.world2Screen(point0, mapInfo);
    var axisDotX = scrnPt1.x + axisLenth;
    var axisDotY = scrnPt1.y;

    map.beginPath();
    map.moveTo(scrnPt1.x, scrnPt1.y);
    map.lineTo(axisDotX, axisDotY);
    map.stroke();
    map.closePath();

    var scaling = mapInfo.getLenthDisScale();
    x = (axisLenth - axisOffGap) / scaling;
    y = axisOffGap / scaling;
    var point3 = { x, y };
    var scrnPt3 = CoordTRFUtil.world2Screen(point3, mapInfo);
    map.beginPath();
    map.moveTo(scrnPt3.x, scrnPt3.y);
    map.lineTo(axisDotX, axisDotY);
    map.stroke();
    map.closePath();

    //-----------------
    x = (axisLenth - axisOffGap) / scaling;
    y = -axisOffGap / scaling;
    var point4 = { x, y };
    var scrnPt4 = CoordTRFUtil.world2Screen(point4, mapInfo);
    map.beginPath();
    map.moveTo(scrnPt4.x, scrnPt4.y);
    map.lineTo(axisDotX, axisDotY);
    map.stroke();
    map.closePath();

    map.restore();
  }



  displayYAxis(map, mapInfo) {
    const axisLenth = 50;
    const axisOffGap = 10;
    const axisLnWid = 3;
    map.save();

    map.beginPath();
    map.lineWidth = 1;
    map.strokeStyle = "#00ff00";

    var x = 0;
    var y = 0;
    var point0 = { x, y };
    var scrnPt1 = CoordTRFUtil.world2Screen(point0, mapInfo);
    var axisDotX = scrnPt1.x;
    var axisDotY = scrnPt1.y - axisLenth;

    map.beginPath();
    map.moveTo(scrnPt1.x, scrnPt1.y);
    map.lineTo(axisDotX, axisDotY);
    map.stroke();
    map.closePath();

    var scaling = mapInfo.getLenthDisScale();
    y = (axisLenth - axisOffGap) / scaling;
    x = axisOffGap / scaling;
    var point3 = { x, y };
    var scrnPt3 = CoordTRFUtil.world2Screen(point3, mapInfo);
    map.beginPath();
    map.moveTo(scrnPt3.x, scrnPt3.y);
    map.lineTo(axisDotX, axisDotY);
    map.stroke();
    map.closePath();

    //-----------------
    y = (axisLenth - axisOffGap) / scaling;
    x = -axisOffGap / scaling;
    var point4 = { x, y };
    var scrnPt4 = CoordTRFUtil.world2Screen(point4, mapInfo);
    map.beginPath();
    map.moveTo(scrnPt4.x, scrnPt4.y);
    map.lineTo(axisDotX, axisDotY);
    map.stroke();
    map.closePath();

    map.restore();
  }


  displayCustCursor(map, mapInfo) {
    const cursorLenth = 12;
    const arrowAngle = 60;
    const arrowLen = 8;
    const arrowGap = 1.5;
    map.save();

    map.beginPath();
    map.lineWidth = 1;
    map.strokeStyle = "#000000";
    map.fillStyle = "#ffffff";

    let cursorPt = mapInfo.getCustCursorPt();
    let cursorVct = mapInfo.getCustCursorVector();
    let particurlarVct = cursorVct.rotateCopy(90);

    let ptlist = [];
    let x = cursorPt.x + cursorLenth * cursorVct.m;
    let y = cursorPt.y + cursorLenth * cursorVct.n;
    let p1 = { x, y };
    x = cursorPt.x - cursorLenth * cursorVct.m;
    y = cursorPt.y - cursorLenth * cursorVct.n;
    let p2 = { x, y };

    ptlist.push(p1);

    //点1右侧
    let vect = cursorVct.invertCopy();
    let rotV = vect.rotateCopy(arrowAngle);
    x = p1.x + arrowLen * rotV.m;
    y = p1.y + arrowLen * rotV.n;
    let pt1 = { x, y };
    ptlist.push(pt1);

    let interpt = funcs.getTwoLineInterPos(pt1, particurlarVct, p1, cursorVct);
    let tmpVect = funcs.getVector(interpt, pt1);
    let ptCross1 = { x: 0, y: 0 };
    ptCross1.x = interpt.x + arrowGap * tmpVect.m;
    ptCross1.y = interpt.y + arrowGap * tmpVect.n;
    ptlist.push(ptCross1);

    //点2左侧
    rotV = cursorVct.rotateCopy(-arrowAngle);
    x = p2.x + arrowLen * rotV.m;
    y = p2.y + arrowLen * rotV.n;
    let pt2 = { x, y };

    interpt = funcs.getTwoLineInterPos(pt2, particurlarVct, p1, cursorVct);
    tmpVect = funcs.getVector(interpt, pt2);
    let ptCross2 = { x: 0, y: 0 };
    ptCross2.x = interpt.x + arrowGap * tmpVect.m;
    ptCross2.y = interpt.y + arrowGap * tmpVect.n;
    ptlist.push(ptCross2);

    ptlist.push(pt2);
    ptlist.push(p2);

    //点2右侧
    rotV = cursorVct.rotateCopy(arrowAngle);
    x = p2.x + arrowLen * rotV.m;
    y = p2.y + arrowLen * rotV.n;
    let pt3 = { x, y };

    interpt = funcs.getTwoLineInterPos(pt3, particurlarVct, p1, cursorVct);
    tmpVect = funcs.getVector(interpt, pt3);
    let ptCross3 = { x: 0, y: 0 };
    ptCross3.x = interpt.x + arrowGap * tmpVect.m;
    ptCross3.y = interpt.y + arrowGap * tmpVect.n;
    ptlist.push(pt3);
    ptlist.push(ptCross3);

    //点1左侧
    vect = cursorVct.invertCopy();
    rotV = vect.rotateCopy(-arrowAngle);
    x = p1.x + arrowLen * rotV.m;
    y = p1.y + arrowLen * rotV.n;
    let pt4 = { x, y };

    interpt = funcs.getTwoLineInterPos(pt4, particurlarVct, p1, cursorVct);
    tmpVect = funcs.getVector(interpt, pt4);
    let ptCross4 = { x: 0, y: 0 };
    ptCross4.x = interpt.x + arrowGap * tmpVect.m;
    ptCross4.y = interpt.y + arrowGap * tmpVect.n;
    ptlist.push(ptCross4);
    ptlist.push(pt4);

    let stPt = ptlist[0];
    let stScrnPt = CoordTRFUtil.world2Screen(stPt, mapInfo);
    map.beginPath();
    map.moveTo(stScrnPt.x, stScrnPt.y);
    for (let i = 1; i < ptlist.length; i++) {
      let tmpPt = ptlist[i];
      let screenPt = CoordTRFUtil.world2Screen(tmpPt, mapInfo);
      map.lineTo(screenPt.x, screenPt.y);
    }
    map.closePath();
    map.stroke();
    map.fill();

    map.restore();
  }

  paint4ExportPng(pngWid, pngHgt, exportBackground, actmap, datamng) {
    if (!actmap || !datamng) {
      return;
    }

    if (pngWid == 0 || pngHgt == 0) {
      pngWid = this.width;
      pngHgt = this.height;
    }

    var mapInfo = datamng.getMapViewInfo();
    let backUpScaling = mapInfo.getScaling();
    let backUpViewPort = mapInfo.getViewPort();
    this.scaleAllGraph2ViewPort(datamng, mapInfo, pngWid, pngHgt);
    let newViewPort = mapInfo.getViewPort();
    let layerMng = datamng.getLayerManager();
    actmap.clearRect(0, 0, pngWid, pngHgt);
    if (exportBackground) {
      this.displayBackGround(actmap, mapInfo);
    }
    layerMng.display(actmap, mapInfo, newViewPort);
    mapInfo.resizeMap(this.width, this.height);
    mapInfo.setScaling(backUpScaling);
    mapInfo.setViewPort(backUpViewPort.left, backUpViewPort.top, backUpViewPort.right, backUpViewPort.bottom);
    mapInfo.setPaint4PngExport(false);
  }

  scaleAllGraph2ViewPort(datamng, mapInfo, width, height) {
    let allGraph = datamng.getAllVisibleGraphs();
    let rect = CommonSvc.calGraphsZone(allGraph);
    let graphWid = rect.width();
    let graphHgt = rect.height();
    let scalex = width / graphWid;
    let scaley = height / graphHgt;
    let scale = Math.min(scalex, scaley);
    let cnt = rect.getCnt();
    let left = cnt.x - width / (scale * 2);
    let right = cnt.x + width / (scale * 2);
    let top = cnt.y + height / (scale * 2);
    let bottom = cnt.y - height / (scale * 2);
    mapInfo.setScaling(scale);
    mapInfo.resizeMap(width, height);
    // let worldMapRect = new LGXCOM.LGXRect(0, height, width, 0);
    // mapInfo.resetWorldMap(worldMapRect)
    mapInfo.setViewPort(left, top, right, bottom);
  }

  displaySimulateToolBarOnMap(custToolBar, frontPainter, mapInfo) {
    this.displayBackGround(this.backGroundMap, mapInfo);
    this.backGroundMap.save();
    this.backGroundMap.globalCompositeOperation = 'source-over';
    this.backGroundMap.drawImage(this.bufferCanvas, 0, 0);

    this.panBufferMap.clearRect(0, 0, this.width, this.height);
    custToolBar.display(this.panBufferMap, mapInfo);
    this.backGroundMap.drawImage(this.panBufferCanvas, 0, 0);
    this.backGroundMap.restore();

    frontPainter.save();
    frontPainter.clearRect(0, 0, this.width, this.height);
    frontPainter.globalCompositeOperation = 'source-over';
    frontPainter.drawImage(this.backGroundCanvas, 0, 0);
    frontPainter.restore();
  }

  rePaintAuxGraphs(actmap, datamng) {
    if (!actmap || !datamng) {
      return;
    }
    this.backGroundMap.clearRect(0, 0, this.width, this.height);
    let mapInfo = datamng.getMapViewInfo();
    this.displayBackGround(this.backGroundMap, mapInfo);
    var layerMng = datamng.getLayerManager();
    let subDataMngLst = datamng.getSubDataMngs();

    this.backGroundMap.save();
    this.backGroundMap.globalCompositeOperation = 'source-over';
    this.backGroundMap.drawImage(this.bufferCanvas, 0, 0);
    layerMng.displayAuxGraphs(subDataMngLst, this.backGroundMap, mapInfo);
    this.dislayTopoErrorGraph(this.backGroundMap, mapInfo, datamng);
    if (mapInfo.getShowCustCursor()) {
      this.displayCustCursor(this.backGroundMap, mapInfo);
    }
    actmap.clearRect(0, 0, this.width, this.height);
    actmap.drawImage(this.backGroundCanvas, 0, 0);
    this.backGroundMap.restore();
  }

}

export default { DisplayMng }
